using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using cframework;
using System.ComponentModel;
using Newtonsoft.Json;
using System.Drawing.Drawing2D;

namespace cframework
{
    [CreateAssetMenu(menuName = "cframework/FileSystemUtil_LocalHttp")]
    public class FileSystemUtil_LocalHttp : FileSystemUtil_Local
    {
        [Essential]
        public int port = 1234;

        public override string GetDownloadUrl()
        {
            return SimpleHTTPServer.GetHttpUrl(port);
        }

        [MenuItem("Assets/cframework/HttpServer/Start")]
        public static void MenuStartHttpServer()
        {
            FileSystemUtil_LocalHttp fshu;
            var setting = Selection.activeObject as BuildPlayerSetting;
            if (setting == null)
            {
                fshu = Selection.activeObject as FileSystemUtil_LocalHttp;
            }
            else
            {
                fshu = setting.fileSystemUtil as FileSystemUtil_LocalHttp;
            }

            if (fshu == null)
            {
                Debug.LogError("FileSystemHttpUtil not found");
                return;
            }

            fshu.StartHttpServer();
        }


        [MenuItem("Assets/cframework/PythonHttpServer/Start")]
        public static void MenuStartPythonHttpServer()
        {
            FileSystemUtil_LocalHttp fshu;
            var setting = Selection.activeObject as BuildPlayerSetting;
            if (setting == null)
            {
                fshu = Selection.activeObject as FileSystemUtil_LocalHttp;
            }
            else
            {
                fshu = setting.fileSystemUtil as FileSystemUtil_LocalHttp;
            }

            if (fshu == null)
            {
                Debug.LogError("FileSystemHttpUtil not found");
                return;
            }

            fshu.StartPythonHttpServer();
        }

        public void StartHttpServer()
        {
            FileSystemUtil_LocalHttp fshu = this;

            SimpleHTTPServer.StartHttpServer(fshu.GetLocalPath(), fshu.port);
        }


        public void StartPythonHttpServer()
        {
            FileSystemUtil_LocalHttp fshu = this;
            PythonHttpServer.StartServer(fshu.GetLocalPath(), fshu.port);
        }

        [MenuItem("Assets/cframework/HttpServer/Stop")]
        [MenuItem("cframework/HttpServer/Stop")]
        public static void StopHttpServer()
        {
            SimpleHTTPServer.StopHttpServer();
        }
    }
}